//
//  MKUIPickerViewModel.m
//  MKUI
//
//  Created by sushangjin on 15/6/18.
//  Copyright (c) 2015年 sushangjin. All rights reserved.
//

#import "MKUIPickerViewModel.h"
#import "MKUIPickerViewCellView.h"

@implementation MKUIPickerViewModel
- (id)init{
	if (self=[super init]) {
		self.allowsSelection = YES;
		self.allowsMultipleSelection = YES;
	}
	return self;
}
- (id)initWithPickerView:(nullable UIPickerView *)pickerView{
    if (self=[self init]) {
        [self setPickerViewDataSourceAndDelegate:pickerView];
    }
    return self;
}
- (void)setPickerViewDataSourceAndDelegate:(UIPickerView *)pickerView{
	self.pickerView = pickerView;
	self.pickerView.dataSource = self;
	self.pickerView.delegate = self;
}
- (MKCollectionSectionModel *)createEmptySectionModel{
	MKUIPickerViewComponentModel *section = [[MKUIPickerViewComponentModel alloc] init];
	return section;
}
- (void)reloadPickerViewData{
	for (MKUIPickerViewComponentModel *sm in self.sectionModels) {
		if([sm cellModelsForSelectedCellModels].count==0){
			MKUIPickerViewCellModel *cm = [sm cellModelAtIndex:0];
			cm.selected = YES;
		}
	}
	[self.pickerView reloadAllComponents];
	[self selectPickerViewAnimated:NO];
}
- (void)selectPickerViewAnimated:(BOOL)animated{
	NSArray *indexPaths = [self indexPathsForSelectedCellModels];
	for (NSIndexPath *indexpath in indexPaths) {
		[self.pickerView selectRow:indexpath.row inComponent:indexpath.section animated:animated];
	}
}
#pragma mark - select
- (void)selectCellModel:(MKCollectionCellModel *)cellModel{
	MKUIPickerViewComponentModel *componentModel = (MKUIPickerViewComponentModel *)cellModel.sectionModel;
	NSArray *cellModels = [componentModel cellModels];
	cellModel.selected = YES;
	for (MKUIPickerViewCellModel *cm in cellModels) {
		if(![cm isEqual:cellModel]){
			cm.selected = NO;
		}
	}
}
- (void)selectCellModels:(NSArray *)cellModels{
	for (MKCollectionCellModel *cellModel in cellModels) {
		[self selectCellModel:cellModel];
	}
}
- (NSArray *)indexPathsForCellModelsWithTitles:(NSArray *)titles{
	NSMutableArray *indexpaths = [[NSMutableArray alloc] initWithCapacity:titles.count];
	for (int i=0; i<titles.count; i++) {
		NSString *title = titles[i];
		MKUIPickerViewComponentModel *sectionModel = (MKUIPickerViewComponentModel *)[self sectionModelAtIndex:i];
		NSInteger index = [sectionModel indexOfCellModelWithTitle:title];
		NSIndexPath *indexpath = [NSIndexPath indexPathForRow:index inSection:i];
		[indexpaths addObject:indexpath];
	}
	return indexpaths;
}
#pragma mark - delegate:UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
	if([self.forwardDataSource respondsToSelector:_cmd]){
		return [self.forwardDataSource numberOfComponentsInPickerView:pickerView];
	}
	NSInteger number = [self numberOfSections];
	return number;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
	if([self.forwardDataSource respondsToSelector:_cmd]){
		return [self.forwardDataSource pickerView:pickerView numberOfRowsInComponent:component];
	}
	MKUIPickerViewComponentModel *componentModel = [self sectionModelAtIndex:component];
	NSInteger number = [componentModel numberOfCells];
	return number;
}
#pragma mark - delegate:UIPickerViewDelegate
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
	if([self.forwardDelegate respondsToSelector:_cmd]){
		return [self.forwardDelegate pickerView:pickerView widthForComponent:component];
	}
	CGRect bounds = pickerView.bounds;
	MKUIPickerViewComponentModel *componentModel = [self sectionModelAtIndex:component];
	CGFloat width = componentModel.cellSize.width;
	if(width==0){
		width = bounds.size.width/[self numberOfComponentsInPickerView:pickerView];
	}
	return width;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
	if([self.forwardDelegate respondsToSelector:_cmd]){
		return [self.forwardDelegate pickerView:pickerView rowHeightForComponent:component];
	}
	MKUIPickerViewComponentModel *componentModel = [self sectionModelAtIndex:component];
	CGFloat height = componentModel.cellSize.height;
	if(height==0){
		height = 44;
	}
	return height;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
	if([self.forwardDelegate respondsToSelector:_cmd]){
		return [self.forwardDelegate pickerView:pickerView titleForRow:row forComponent:component];
	}
	MKUIPickerViewCellModel *cellModel = [self cellModelAtIndexPath:[NSIndexPath indexPathForRow:row inSection:component]];
	NSString *title = cellModel.title;
	return title;
}
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component NS_AVAILABLE_IOS(6_0){
	if([self.forwardDelegate respondsToSelector:_cmd]){
		return [self.forwardDelegate pickerView:pickerView attributedTitleForRow:row forComponent:component];
	}
	MKUIPickerViewCellModel *cellModel = [self cellModelAtIndexPath:[NSIndexPath indexPathForRow:row inSection:component]];
	NSAttributedString *attributedTitle = cellModel.attributedTitle;
	return attributedTitle;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
	if([self.forwardDelegate respondsToSelector:_cmd]){
		return [self.forwardDelegate pickerView:pickerView viewForRow:row forComponent:component reusingView:view];
	}
	MKUIPickerViewCellModel *cellModel = [self cellModelAtIndexPath:[NSIndexPath indexPathForRow:row inSection:component]];
	Class c = cellModel.cellClass;
	UIView <MKUIPickerViewCellProtocol>* cell;
	if(!c){
		c = [MKUIPickerViewCellView class];
	}
	if(c){
		CGRect bounds = pickerView.bounds;
		CGRect f1 = bounds;
		f1.size = [pickerView rowSizeForComponent:component];
		if([view isKindOfClass:c]){
			cell = (UIView <MKUIPickerViewCellProtocol>*)view;
		}else{
			cell = [[c alloc] initWithFrame:f1];
		}
		cell.bounds = f1;
		[cellModel displayCell:cell];
		return cell;
	}
	return [UIView new];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    MKUIPickerViewCellModel *cellModel = [self cellModelAtIndexPath:[NSIndexPath indexPathForRow:row inSection:component]];
    [self selectCellModel:cellModel];
    [cellModel didSelected:pickerView];
    if([self.forwardDelegate respondsToSelector:_cmd]){
        [self.forwardDelegate pickerView:pickerView didSelectRow:row inComponent:component];
    }
}
@end

@implementation MKUIPickerViewModel(SelectAnimated)
- (void)selectCellModelAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated{
    [self selectCellModel:[self cellModelAtIndexPath:indexPath] animated:animated];
}
- (void)selectCellModelsAtIndexPaths:(nullable NSArray<NSIndexPath *> *)indexPaths animated:(BOOL)animated{
    for (NSIndexPath *indexPath in indexPaths) {
        [self selectCellModelAtIndexPath:indexPath animated:animated];
    }
}
- (void)selectCellModel:(nullable MKCollectionCellModel *)cellModel animated:(BOOL)animated{
    [self selectCellModel:cellModel];
    NSIndexPath *indexPath = cellModel.indexPathInModel;
    [self.pickerView selectRow:indexPath.row inComponent:indexPath.section animated:animated];
}
- (void)selectCellModels:(nullable NSArray<MKCollectionCellModel *> *)cellModels animated:(BOOL)animated{
    for (MKCollectionCellModel *cellModel in cellModels) {
        [self selectCellModel:cellModel animated:animated];
    }
}
@end
